package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.fans;

import android.view.View;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.MyApplication;
import com.kaiwukj.android.ufamily.mvp.helper.AccountStore;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.FansResult;
import com.qmuiteam.qmui.widget.roundwidget.QMUIRoundButton;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class FansAdapter extends BaseQuickAdapter<FansResult, BaseViewHolder> {

    private int type;
    private boolean isOwner = false;

    public void setAttention() {
        type = 1;
    }

    public void setIsOwner(boolean flag) {
        isOwner = flag;
    }

    public void postAttention(int position) {
        getData().get(position).setConcernFans(1);
        notifyItemChanged(position, "attention");
    }

    public void postCancelAttention(int position) {
        getData().get(position).setConcernFans(0);
        notifyItemChanged(position, "attention");
    }

    public FansAdapter() {
        super(R.layout.item_fans, new ArrayList<>());
        addChildClickViewIds(R.id.iv_avatar);
    }

    @Override
    public long getItemId(int position) {
        FansResult item = getItem(position);
        return item == null ? 0 : item.getId();
    }

    public long getLastItemId() {
        return getData().size() == 0 ? 0 : getItemId(getData().size() - 1);
    }

    @Override
    public void onBindViewHolder(@NotNull BaseViewHolder holder, int position, @NotNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            String tag = (String) payloads.get(0);
            if ("attention".equals(tag)) {
                View containerAttentionEnable = holder.getView(R.id.container_attention);
                QMUIRoundButton btnAttention = holder.getView(R.id.btn_attention);
                if (type == 1) {
                    if (getData().get(position).isFans()) {
                        containerAttentionEnable.setVisibility(View.GONE);
                        btnAttention.setVisibility(View.VISIBLE);
                        btnAttention.setText("已关注");
                        btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.gray_f2));
                    } else {
                        btnAttention.setText("关注");
                        btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
                    }

                } else {
                    btnAttention.setText("相互关注");
                    if (getData().get(position).isFans()) {
                        containerAttentionEnable.setVisibility(View.GONE);
                        btnAttention.setVisibility(View.VISIBLE);
                    } else {
                        containerAttentionEnable.setVisibility(View.VISIBLE);
                        btnAttention.setVisibility(View.GONE);
                        holder.setText(R.id.btn_attention_enable, "回关");
                    }
                }
            }
        }
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, FansResult result) {
        ImageView ivAvatar = holder.getView(R.id.iv_avatar);
        Glide.with(getContext())
                .load(result.getHeadImg())
                .placeholder(R.mipmap.icon_common_head_place_holder)
                .into(ivAvatar);

        holder.setText(R.id.tv_name, result.getNickname());
        holder.setText(R.id.tv_desc, result.getSignature());
        //  holder.setText(R.id.)

        // addChildClickViewIds(R.id.container_attention, R.id.btn_attention);

        View containerAttentionEnable = holder.getView(R.id.container_attention);
        QMUIRoundButton btnAttention = holder.getView(R.id.btn_attention);
        btnAttention.setOnClickListener(v -> {//取消关注
            if (type != 1) {
                //  btnAttention.setVisibility(View.GONE);
                //  containerAttentionEnable.setVisibility(View.VISIBLE);
            }
            if (mOnAttentionListener != null) {
                mOnAttentionListener.onAttention(holder.getLayoutPosition(), result.isFans());
            }
        });

        containerAttentionEnable.setOnClickListener(v -> {//关注
            // btnAttention.setVisibility(View.VISIBLE);
            //containerAttentionEnable.setVisibility(View.GONE);
            if (mOnAttentionListener != null) {
                mOnAttentionListener.onAttention(holder.getLayoutPosition(), result.isFans());
            }
        });

        if (type == 1) {
            if (result.isFans()) {
                containerAttentionEnable.setVisibility(View.GONE);
                btnAttention.setVisibility(View.VISIBLE);
                btnAttention.setText("已关注");
                btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.gray_f2));
            } else {
                btnAttention.setText("关注");
                btnAttention.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorAccent));
            }
            if (result.getBeConcernId().equals(AccountStore.getUserResult().getId())) {
                btnAttention.setVisibility(View.GONE);
                containerAttentionEnable.setVisibility(View.GONE);
            }

        } else {
            btnAttention.setText("相互关注");
            if (result.isFans()) {
                containerAttentionEnable.setVisibility(View.GONE);
                btnAttention.setVisibility(View.VISIBLE);
            } else {
                containerAttentionEnable.setVisibility(View.VISIBLE);
                btnAttention.setVisibility(View.GONE);
                holder.setText(R.id.btn_attention_enable, "回关");
            }
            if (result.getConcernId().equals(MyApplication.getInstance().getUserInfo().getId())) {
                containerAttentionEnable.setVisibility(View.GONE);
                btnAttention.setVisibility(View.GONE);
            }
        }


    }

    private OnAttentionListener mOnAttentionListener;

    public void setOnAttentionListener(OnAttentionListener listener) {
        mOnAttentionListener = listener;
    }

    public interface OnAttentionListener {
        void onAttention(int pos, boolean attention);
    }
}
